package com.onec.service.algorithm;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 给定 nums = [2, 7, 11, 15], target = 9
 *
 * @result nums[0] + nums[1] = 2 + 7 = 9
 */
public class Solution {

  public static int[] twoSum(int[] nums, int target) {

    int size = nums.length;
    for (int i = 0; i <= size - 1; i++) {
      for (int x = i + 1; x <= size - 1; x++) {
        int sum = nums[i] + nums[x];
        if (target == sum) {
          return new int[]{i, x};
        }
      }
    }
    return null;
  }

  public static void main(String[] args) {
    System.out.println(System.currentTimeMillis());
    System.out.println(Arrays.toString(twoSum(new int[]{1, 2, 3, 4, 5, 6}, 11)));
    System.out.println("执行时间:\r\n" + System.currentTimeMillis());
  }


  /**
   * 两数相加
   */
  public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

    return l1;
  }

  //   连续的最长的数
  public int lengthOfLongestSubstring(String s) {
    int n = s.length(), ans = 0;
    Map<Character, Integer> map = new HashMap<>();
    for (int end = 0, start = 0; end < n; end++) {
      char alpha = s.charAt(end);
      if (map.containsKey(alpha)) {
        start = Math.max(map.get(alpha), start);
      }
      ans = Math.max(ans, end - start + 1);
      map.put(s.charAt(end), end + 1);
    }
    return ans;
  }
}

class ListNode {

  int val;
  ListNode next;

  ListNode() {
  }

  ListNode(int val) {
    this.val = val;
  }

  ListNode(int val, ListNode next) {
    this.val = val;
    this.next = next;
  }
}
